package com.ak.server.platform.controller;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;

import com.ak.common.entity.AKResponse;
import com.ak.common.entity.Enums;
import com.ak.common.entity.system.Application;
import com.ak.common.utils.EnumUtil;
import com.ak.server.platform.service.IApplicationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@Api(tags = "子系统模块")
@RestController
@RequestMapping("application")
public class ApplicationController extends BaseController {

    @Autowired
    private IApplicationService applicationService;

    /**
     * 系统管理列表
     *
     * @param application
     * @return
     */
    @ApiOperation("分页查询子系统")
    @LogEndpoint(operation = "分页查询子系统")
    @GetMapping
    public AKResponse list(Page page, Application application) {
        return new AKResponse().data(applicationService.page(page, new LambdaQueryWrapper<Application>(application).orderByAsc(Application::getSort)));
    }

    @ApiOperation("查询所有子系统")
    @LogEndpoint(operation = "查询所有子系统")
    @GetMapping("/getApplications")
    public AKResponse getApplications() {
        return new AKResponse().data(applicationService.list());
    }

    @ApiOperation("查询子系统编码字典")
    @GetMapping("/appCodes")
    public AKResponse appCodes() {
        return new AKResponse().data(EnumUtil.getEnumToJSONArray(Enums.ApplicationCodes.class));
    }

    @ApiOperation("查询子系统类型枚举")
    @GetMapping("/appTypes")
    public AKResponse appTypes() {
        Map<String, String> map = EnumUtil.EnumToMap(Enums.ApplicationType.class);
        return new AKResponse().data(map);
    }

    /**
     * @param application
     * @return
     */
    @ApiOperation("添加子系统")
    @LogEndpoint(operation = "添加子系统")
    @PostMapping
    public AKResponse add(@Validated Application application) {
        application.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        application.setCreateTime(new Date());
        return new AKResponse().data(applicationService.save(application));
    }

    /**
     * 校验编码
     */
    @GetMapping("/checkCodeExist/{appCode}")
    @ResponseBody
    public boolean checkCodeExist(@PathVariable("appCode") String appCode) {
        return applicationService.checkAppCodeUnique(appCode) > 0 ? true : false;
    }

    @ApiOperation("更新子系统")
    @LogEndpoint(operation = "更新子系统")
    @PutMapping
    public AKResponse update(@Validated Application application) {
        application.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        application.setUpdateTime(new Date());
        return new AKResponse().data(applicationService.updateById(application));
    }

    /**
     * 物理删除
     * @param id
     * @return
     */
    @ApiOperation("删除子系统")
    @LogEndpoint(operation = "删除子系统")
    @DeleteMapping("/{id}")
    public AKResponse delete(@PathVariable("id") String id) {
        return new AKResponse().data(applicationService.removeById(id));
    }


}
